package com.common.dongqiuhui.apps.comm.view.inputemoji;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import width.hgk.wanwan.R;

/**
 * 自定义
 * @author fyh
 *
 */
public class EmojiView extends RelativeLayout {
	private ViewPager viewPager;
	private EmojiFlowIndicatorView mFlowIndicator;
	private ArrayList<GridView> grids = new ArrayList<GridView>();
	private int[] emoji_id_list ;
            //EmojiContants.emoji_ids;
	private String[] emoji_id_name;
	private int page = 0;
	private final int r = 7;
	private final int c = 3;
	private Context context;
	private EmojiViewOnItemClickListener onItemClickListener;
	public EmojiView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
	}

	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		viewPager=(ViewPager)findViewById(R.id.emoji_viewpager);
		mFlowIndicator=(EmojiFlowIndicatorView)findViewById(R.id.emoji_flowIndicato);
//			emoji_id_name=EmojiContants.getEmojis();
		//EmojiContants emoji=EmojiContants.creat();
		//emoji_id_name=emoji.getEmojiUtf8();
		if (emoji_id_name == null) {
			// TODO 出错处理
			Toast.makeText(context, "读取表情出错 erro -1", Toast.LENGTH_SHORT).show();
			return;
		}
		getPageNum();
		initGridView();
	}

	private PagerAdapter mPagerAdapter = new PagerAdapter() {

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public int getCount() {
			return grids.size();
		}

		@Override
		public void destroyItem(View container, int position, Object object) {
			((ViewPager) container).removeView(grids.get(position));
		}

		@Override
		public Object instantiateItem(View container, int position) {
			((ViewPager) container).addView(grids.get(position));
			return grids.get(position);
		}
	};

	private void getPageNum() {
		if ((emoji_id_list.length) % (r * c) == 0) {
			page = (emoji_id_list.length) / (r * c);
		} else {
			page = (emoji_id_list.length) / (r * c) + 1;
		}
	}

	private void initGridView() {
		mFlowIndicator.setCount(page);
		int num = 0;
		for (int i = 0; i < page; i++) {
			List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
			for (int j = 0; j < c * r; j++) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("PIC", emoji_id_list[num]);
				num++;
				list.add(map);
				if (num > emoji_id_list.length) {
					return;
				}
			}
			GridView gView = (GridView) LayoutInflater.from(context).inflate(R.layout.emoji_grid, null);
			gView.setSelector(new ColorDrawable(Color.TRANSPARENT));
			gView.setNumColumns(r);
			SimpleAdapter adapter = new SimpleAdapter(context, (List<Map<String, Object>>) list,
					R.layout.emoji_grid_item, new String[] { "PIC", }, new int[] { R.id.emoji_item_image });
			gView.setAdapter(adapter);
			grids.add(gView);
		}
		
		for(int i=0;i<grids.size();i++){
			final int gridsId=i;
			grids.get(i).setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
					if(onItemClickListener!=null){
						int itemId=0;
						itemId=gridsId*r*c+arg2;
						try {
							onItemClickListener.onItemClick(itemId, emoji_id_name[itemId],emoji_id_list[itemId]);
						} catch (Exception e) {
							e.printStackTrace();
							//TODO --异常
							Toast.makeText(context, "读取表情出错 erro -2",
									Toast.LENGTH_SHORT).show();
						}
					}
				}
			});
		}
		
		viewPager.setAdapter(mPagerAdapter);
		viewPager.setOnPageChangeListener(new PageChangeListener());
	}
	
	/**
	 * 设置View点击事件监听
	 * @param onItemClickListener
	 */
	public void setOnItemClickListener(EmojiViewOnItemClickListener onItemClickListener) {
		this.onItemClickListener = onItemClickListener;
	}

	private class PageChangeListener implements OnPageChangeListener {
		@Override
		public void onPageScrollStateChanged(int arg0) {
		}
		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}
		@Override
		public void onPageSelected(int arg0) {
			
			mFlowIndicator.setSeletion(arg0);
		}
		
	}
}
